package com.adapter;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dstats.R;
import com.entities.Match;
import com.entities.Player;
import com.entities.Team;

public class TIMatchsAdapter extends BaseAdapter {

	private List<Match> data;
	private Context mContext;
	private LayoutInflater mInflater;
	private  List<Team> teams;
	static class ViewHolder{
	private TextView nameTeamA;
	private TextView nameTeamB;
	private ImageView imgA;
	private ImageView imgB;
	}
	
	public TIMatchsAdapter(Context mContext, List<Match> data){
		mInflater = LayoutInflater.from(mContext);
		this.data = data;
        this.mContext = mContext; 
        mInflater = LayoutInflater.from(mContext);  
	}
	
	private List<Team> getTeamList(){
		if(teams==null){
			try {
				teams = Team.getAllTeams(mContext.getAssets().open("teams.json"));

			} catch (IOException e) {
			}
			}
			return teams;
		
	
	}
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Match getItem(int arg0) {
		return data.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return data.get(arg0).getMatch_id();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.ti_match_row_layout, null);
			holder = new ViewHolder();
			holder.imgA = (ImageView) convertView.findViewById(R.id.imageViewTeamA);
			holder.imgB = (ImageView) convertView.findViewById(R.id.imageViewTeamB);
			holder.nameTeamA = (TextView) convertView.findViewById(R.id.textViewTeamNameA);
			holder.nameTeamB = (TextView) convertView.findViewById(R.id.textViewTeamNameB);
			  convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		Match match = data.get(position);
		Long slot0Id = null;
		Long slot128Id = null;
		for(Player p: match.getPlayers()){
			if(p.getPlayer_slot()==0){
				slot0Id = p.getAccount_id();
			} else if(p.getPlayer_slot()==128){
				slot128Id = p.getAccount_id();
			}
		}
		
		Team teamA = null;
		Team teamB = null;
		if(slot0Id!=null && slot128Id !=null){
		for(Team team: getTeamList()){
			if(team.checkPlayer(slot128Id)){
				teamB = team;
			} if(team.checkPlayer(slot0Id)){
				teamA = team;
			}
		}
		if(teamA !=null && teamB != null){
			holder.nameTeamA.setText(teamA.getTag());
			holder.nameTeamB.setText(teamB.getTag());
			int imgIdentifierA = mContext.getResources().getIdentifier("logo_" + teamA.getLogo().toString(), "drawable", mContext.getPackageName());
			int imgIdentifierB = mContext.getResources().getIdentifier("logo_" + teamB.getLogo().toString(), "drawable", mContext.getPackageName());
			if(imgIdentifierA!=0){
				Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), imgIdentifierA);
				holder.imgA.setImageBitmap(Bitmap.createScaledBitmap(bm, 103, 55, false));
			}
			if(imgIdentifierB!=0){
				Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), imgIdentifierB);
				holder.imgB.setImageBitmap(Bitmap.createScaledBitmap(bm, 103, 55, false));
			}
			
		} else {
			Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.white);
			Bitmap createScaledBitmap = Bitmap.createScaledBitmap(bm, 103, 55, false);
			holder.imgA.setImageBitmap(createScaledBitmap);
			holder.imgB.setImageBitmap(createScaledBitmap);
			holder.nameTeamA.setText(mContext.getString(R.string.radiant));
			holder.nameTeamB.setText(mContext.getString(R.string.dire));
			
		}
		
		}
		
		return convertView;
	}

}
